=pod
 * Rack.pm
 * 
 * Copyright (C) 2012 Elena Shishkina
 * 
 * This file is part of ServerInventory.
 * 
 * ServerInventory is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ServerInventory is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ServerInventory.  If not, see <http://www.gnu.org/licenses/>.
=cut

use utf8;
use 5.12.0;
package ServerInventory::Rack;
use Moose;
use DBI;
use JSON;

sub Process {
    my $self = shift;
    my $r = shift;
    $self->{request} = $r;
    given ($r->param('mode')) {
        when ('edit') { $self->Edit }
        when ('remove') { $self->Remove }
        when ('add') { $self->Add }
        when ('full') { $self->FullData }
        when ('list') { $self->GetList }
        when ('move') { $self->Move }
        when ('lock') { $self->Lock }
        when ('unlock') { $self->Unlock }
        default { $self->LoadData }
    }
}

sub FullData {
    my $self = shift;
    my $r = $self->{request};
    my $data = $r->{dbh}->selectall_arrayref(qq{select r.*, d.name as dc, v.name as vendor
from racks r
left join dcs d on r.dc_id = d.id
left join vendors v on r.vendor_id = v.id
order by d.name, r.name}, {Slice => {}}) || [];
    $r->render(json => $data);
}

1;
